import java.util.Random;

public class ConstructorTest {
    public static void main(String[] args) {
        var staff = new Employee[3];

        staff[0] = new Employee("Harry", 40000);
        staff[1] = new Employee(60000);
        staff[2] = new Employee();

        // 输出每个Employee的信息
        for (Employee e : staff) {
            System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
        }

    }
}

class Employee {
    private static int nextId;

    private int id;
    private String name = "";  // 实例字段初始化
    private double salary;

    private static Random generator = new Random();

    // 静态初始化块
    static {
        // 设置nextId为一个0-9999的随机值
        nextId = generator.nextInt(10000);
    }

    // 对象初始化块
    {
        id = nextId;
        nextId++;
    }

    // 三个重载的构造器

    public Employee() {
        // name被初始化为"", 参见上面
        // salary没有显式设置，被初始化为0
        // id在对象初始化块中初始化
    }

    public Employee(double s) {
        // 调用Employee(String, double)构造器
        this("Employee #" + nextId, s);
    }

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public int getId() {
        return id;
    }
}